package org.jypj.dev.controller;

import org.jypj.dev.model.Menu;
import org.jypj.dev.model.User;
import org.jypj.dev.service.ICodeDataService;
import org.jypj.dev.service.IMenuService;
import org.jypj.dev.service.IUserService;
import org.jypj.dev.util.PropertyUtil;
import org.jypj.dev.util.StringUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class LoginController {

    @Resource
    private IUserService userService;

    @Resource
    private IMenuService menuService;

    @Resource
    private ICodeDataService codeDataService;


    /**
     * 进入登录页面
     *
     * @return
     */
    @RequestMapping(value = "/toLogin.action")
    public String toLogin(HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user != null) {
            return "redirect:toMain.action";
        }
        return "login";
    }

    /**
     * 检查用户名密码 并设置session中
     *
     * @param loginname
     * @param password
     * @return -1 表示用户名不存在 -2表示密码不正确
     */
    @RequestMapping("checkUser.action")
    @ResponseBody
    public Map<String, Object> checkUser(String loginname, String password, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        int code = 0;
        if (StringUtil.isNotEmpty(loginname)) {
            List<User> users = userService.selectAllByUser(new User(loginname, ""));
            if (users == null || users.size() == 0) {
                code = -1;
            } else {
                User user = users.get(0);
                String password2 = user.getPassword();
                if (!password2.equals(password)) {
                    code = -2;
                } else {
                    request.getSession().setAttribute("user", user);
                }
            }
        } else {
            code = -2;
        }
        map.put("code", code);
        return map;
    }

    /**
     * 进入首页
     *
     * @return
     */
    @RequestMapping(value = "/toMain.action")
    public String toMain(HttpServletRequest request) {
        try {
            User user = (User) request.getSession().getAttribute("user");
            if (user != null) {
                if (StringUtil.isEmpty(user.getRoleType())) {
                    return "noauth";
                }
                String roleName = codeDataService.selectItemTextByItemValue("jsgl", user.getRoleType());
                user.setRoleName(roleName);

                Map<String, Object> queryMap = new HashMap<>();
                List<Menu> menus = null;
                if (!"0000".equals(user.getRoleType())) {
                    queryMap.put("roleType", user.getRoleType());
                }
                menus = menuService.selectAllByMap(queryMap);

                //如果该角色没有菜单就提示无权访问
                if (menus == null || menus.size() == 0) {
                    return "noauth";
                }
                user.setMenus(menus);
            }
            String fileServerUrl = PropertyUtil.getProperties("file_server_url");
            request.getSession().setAttribute("fileServerUrl", fileServerUrl);

        } catch (Exception e) {
            e.printStackTrace();
            return "noauth";
        }
        return "back/main";
    }

    /**
     * 注销
     *
     * @return
     */
    @RequestMapping(value = "/logout.action")
    public String logout(HttpServletRequest request) {
        request.getSession().invalidate();
        return "redirect:/toLogin.action";
    }

}
